import cv2
import matplotlib.pyplot as plt

imgBGR = cv2.imread("LR.jpeg", flags = 1)  # flags=1 读取彩色图像(BGR)

# 获取B通道
bImg = imgBGR.copy()  # 获取BGR
bImg[:, :, 1] = 0  # G=0
bImg[:, :, 2] = 0  # R=0

# 获取G通道
gImg = imgBGR.copy()  # 获取BGR
gImg[:, :, 0] = 0  # B=0
gImg[:, :, 2] = 0  # R=0

# 获取R通道
rImg = imgBGR.copy()  # 获取BGR
rImg[:, :, 0] = 0  # B=0
rImg[:, :, 1] = 0  # G=0

plt.subplot(231), plt.title("BGR"), plt.axis('off')
#plt.imshow()和cv2.imshow()颜色模型不一样
plt.imshow(imgBGR)  # matplotlib显示BGR

plt.subplot(233), plt.title("RGB"), plt.axis('off')
imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB)  # BGR--->RGB
plt.imshow(imgRGB)  # matplotlib显示RGB

plt.subplot(234), plt.title("B channel"), plt.axis('off')
bImg = cv2.cvtColor(bImg, cv2.COLOR_BGR2RGB)  # BGR--->RGB
plt.imshow(bImg)  # matplotlib显示channel B

plt.subplot(235), plt.title("G channel"), plt.axis('off')
gImg = cv2.cvtColor(gImg, cv2.COLOR_BGR2RGB)
plt.imshow(gImg)  # matplotlib显示channel G

plt.subplot(236), plt.title("R channel"), plt.axis('off')
rImg = cv2.cvtColor(rImg, cv2.COLOR_BGR2RGB)
plt.imshow(rImg)  # matplotlib显示channel R

plt.tight_layout() #自动调整子图间距
plt.show()